package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.GodCommunity;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.GodCommunityService;
import com.javasm.service.impl.GodCommunityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/godCommunity/*")
public class GodCommunityServlet extends BaseServlet {
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String communityName = req.getParameter("communityName");
        String communityLogo = req.getParameter("communityLogo");
        String communityLeaderStr = req.getParameter("communityLeader");
        Integer communityLeader = null;
        if (communityLeaderStr != null && !"".equals(communityLeaderStr)) {
            communityLeader = Integer.parseInt(communityLeaderStr);
        }
        GodCommunity inputCommunity = new GodCommunity(communityName, communityLogo, communityLeader);
        GodCommunityService godCommunityService = new GodCommunityServiceImpl();
        int resNum = godCommunityService.addGodCommunity(inputCommunity);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String communityIdStr = req.getParameter("communityId");
        Integer communityId = null;
        if (communityIdStr != null && !"".equals(communityIdStr)) {
            communityId = Integer.parseInt(communityIdStr);
        }
        GodCommunityService godCommunityService = new GodCommunityServiceImpl();
        int resNum = godCommunityService.deleteGodCommunityById(communityId);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String communityIdStr = req.getParameter("communityId");
        String communityName = req.getParameter("communityName");
        String communityLogo = req.getParameter("communityLogo");
        String communityLeaderStr = req.getParameter("communityLeader");
        Integer communityLeader = null;
        if (communityLeaderStr != null && !"".equals(communityLeaderStr)) {
            communityLeader = Integer.parseInt(communityLeaderStr);
        }
        Integer communityId = null;
        if (communityIdStr != null && !"".equals(communityIdStr)) {
            communityId = Integer.parseInt(communityIdStr);
        }
        GodCommunity inputCommunity = new GodCommunity(communityId, communityName, communityLogo, communityLeader);
        GodCommunityService godCommunityService = new GodCommunityServiceImpl();
        int resNum = godCommunityService.editGodCommunity(inputCommunity);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void getGodCommunityById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String communityIdStr = req.getParameter("communityId");
        Integer communityId = null;
        if (communityIdStr != null && !"".equals(communityIdStr)) {
            communityId = Integer.parseInt(communityIdStr);
        }
        GodCommunityService godCommunityService = new GodCommunityServiceImpl();
        GodCommunity godCommunity = godCommunityService.getGodCommunityById(communityId);
        ReturnResult rrs = null;
        if (godCommunity != null) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),godCommunity);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 10;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String communityName = req.getParameter("communityName");
        String communityLeaderName = req.getParameter("communityLeaderName");
        GodCommunity inputCommunity = new GodCommunity();
        inputCommunity.setCommunityName(communityName);
        inputCommunity.setCommunityLeaderName(communityLeaderName);
        GodCommunityService godCommunityService = new GodCommunityServiceImpl();
        Integer total = godCommunityService.countCommunitys(inputCommunity);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<GodCommunity> godCommunityList = godCommunityService.listCommunitys(inputCommunity, page, pageSize);

        ReturnResult rrs = null;
        if (godCommunityList.size() > 0) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),godCommunityList);
            PageInfo pageInfo = new PageInfo(page,pageSize,total);
            rrs.setPageInfo(pageInfo);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

}
